package cn.fan.rabbitMQ.listener;

import cn.fan.service.MSGService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MSGListener {
    @Autowired
    private MSGService msgService;

    @RabbitListener(queues = "error.queue")
    public void error(String msg){
        log.error("死信队列"+msg);
    }

    @RabbitListener(queues = "msg.queue")
    public void taskRemind(long id){
        log.info("开始发送邮件");

        Thread t1= new Thread(){
            public void run(){
                try {
                    msgService.sendEmail(id);
                    log.info("发送邮件任务："+id+"已完成!");
                } catch (Exception e) {
                    log.error("发送邮件任务："+id+"失败!");
                }
            }
        };
        t1.start();
    }
}
